package com.tangyuan.common.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Base64;

@Component
@Slf4j
public class PushAppUtil {
    private static final String Authorization = "Basic " + Base64.getEncoder().encodeToString("ae42f043b19a9bb025f7bfdb:030a2f9bd0e0da7cc908b753".getBytes());
    public static Result<ResultCodeEnum> pushInform(String title, String alert){
        String imageUrl = "https://admin.stftt.com/static/img/touxiang.png";
        JSONObject entries = new JSONObject();
        entries.set("platform", Arrays.asList("android","ios"));
        entries.set("audience","all");
        entries.set("inapp_message",new JSONObject().set("inapp_message",false));

        JSONObject android = new JSONObject();
        android.set("alert",alert);
        android.set("title",title);
        android.set("alert_type",7);
        android.set("badge_add_num",1);
        android.set("builder_id",0);
        android.set("category","");
        android.set("extras",new JSONObject());
        android.set("intent",new JSONObject().set("url",""));
        android.set("large_icon",imageUrl);
        android.set("priority",0);
        android.set("sound","");
        android.set("style",0);

        JSONObject ios = new JSONObject();
        ios.set("alert",new JSONObject().set("title",title).set("body",alert));
        ios.set("sound","sound.caf");
        ios.set("badge","+1");
        ios.set("content-available",0);
        ios.set("extras",new JSONObject());
        ios.set("filter-criteria","");
        ios.set("interruption-level","active");
        ios.set("mutable-content","1");
        ios.set("sound","default");
        ios.set("thread-id","default");

        JSONObject options = new JSONObject();
        options.set("apns_production",true);
        entries.set("options",options);

        JSONObject notification = new JSONObject();
        notification.set("alert",title);
        notification.set("android",android);
        notification.set("ios",ios);
        entries.set("notification",notification);

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JSONUtil.toJsonStr(entries));
        Request request = new Request.Builder()
                .url("https://api.jpush.cn/v3/push")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", Authorization)
                .build();
        String bodyResponse = "";
        try {
            Response response = OkHttpUtils.getInstance().newCall(request).execute();
            bodyResponse = response.body().string();
            log.info("推送APP消息成功,ID:{}",bodyResponse);
            return Result.ok();
        }catch (Exception e){
            log.error("推送APP异常:{}",e.toString());
            return Result.fail();
        }
    }
}
